package com.bc.commonentity.log;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.LongSummaryStatistics;

public class LogBean implements Comparable<LogBean> {

	private static DecimalFormat df = new DecimalFormat(".##");   
	
	public LogBean(){}
	public LogBean(String request,Integer type,Long execTime){
		this.request = request;
		this.type = type;
		execTimes.add(execTime);
		this.callCount = 1;
		this.execDate = new Date();
		calcu();
	}
	
	private String request;//请求的url或者redis的key或者sql
	private Integer type;//类型 
	private List<Long> execTimes=new ArrayList<Long>();//请求消耗时间的列表
	private Integer callCount;//调用次数
	
	private double avgTimes; 		//平均耗时
	private Long shortTimes;	//最短耗时
	private Long longestTimes;	//最长耗时
	
	private Date execDate;//请求时间
	
	
	
	public void calcu(){
		if(execTimes.size()>0){
			LongSummaryStatistics longSummaryStatistics=execTimes.stream().mapToLong(t -> t).summaryStatistics();
			this.avgTimes =longSummaryStatistics.getAverage();
			this.shortTimes = longSummaryStatistics.getMin();
			this.longestTimes = longSummaryStatistics.getMax();
		}
	}
	
	public Date getExecDate() {
		return execDate;
	}
	public void setExecDate(Date execDate) {
		this.execDate = execDate;
	}
	public double getAvgTimes() {
		return  Double.valueOf(df.format(avgTimes));
	}
	public void setAvgTimes(double avgTimes) {
		this.avgTimes = avgTimes;
	}
	public Long getShortTimes() {
		return shortTimes;
	}
	public void setShortTimes(Long shortTimes) {
		this.shortTimes = shortTimes;
	}
	public Long getLongestTimes() {
		return longestTimes;
	}
	public void setLongestTimes(Long longestTimes) {
		this.longestTimes = longestTimes;
	}
	public String getRequest() {
		return request;
	}
	public void setRequest(String request) {
		this.request = request;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public List<Long> getExecTimes() {
		return execTimes;
	}
	public void setExecTimes(List<Long> execTimes) {
		this.execTimes = execTimes;
	}
	public Integer getCallCount() {
		return callCount;
	}
	public void setCallCount(Integer callCount) {
		this.callCount = callCount;
	}
	@Override
	public int compareTo(LogBean o) {
		//按照type显示在一起显示
	   return this.getType().compareTo(o.getType());
	}
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	@Override
	public int hashCode() {
		return 37 * request.hashCode();
	}

}
